package com.lhq.lingsi_study_hall.scoring;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lhq.lingsi_study_hall.model.dto.question.QuestionContentDTO;
import com.lhq.lingsi_study_hall.model.entity.App;
import com.lhq.lingsi_study_hall.model.entity.Question;
import com.lhq.lingsi_study_hall.model.entity.ScoringResult;
import com.lhq.lingsi_study_hall.model.entity.UserAnswer;
import com.lhq.lingsi_study_hall.model.vo.QuestionVO;
import com.lhq.lingsi_study_hall.service.QuestionService;
import com.lhq.lingsi_study_hall.service.ScoringResultService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * 自定义评分应用评分策略
 * 例如：随堂测试（有标准的正确和错误答案）
 */
@ScoringStrategyConfig(appType = 0, scoringStrategy = 0)
public class CustomScoreScoringStrategy implements ScoringStrategy {
    @Resource
    private QuestionService questionService;
    @Resource
    private ScoringResultService scoringResultService;
    @Override
    public UserAnswer doScore(List<String> choices, App app) {
        //1. 根据id查询对应的题目和题目结果信息
        Long appId = app.getId();
        Question question = questionService.getOne(
                Wrappers.lambdaQuery(Question.class).eq(Question::getAppId, appId)
        );
        //desc 降序排列
        List<ScoringResult> scoringResultList = scoringResultService.list(
                Wrappers.lambdaQuery(ScoringResult.class)
                        .eq(ScoringResult::getAppId, appId)
                        .orderByDesc(ScoringResult::getResultScoreRange)
        );
        //2. 统计用户的总得分
        int totalScore = 0;
        QuestionVO questionVO = QuestionVO.objToVo(question);
        List<QuestionContentDTO> questionContent = questionVO.getQuestionContent();
        for (QuestionContentDTO questionContentDTO : questionContent) {
            for (String choice : choices) {
                List<QuestionContentDTO.Option> options = questionContentDTO.getOptions();
                for (QuestionContentDTO.Option option : options) {
                    if (choice.equals(option.getKey())){
                        int score= Optional.of(option.getScore()).orElse(0);
                        totalScore += score;
                    }
                }
            }
        }
        //3. 遍历得分结果，找到第一个用户分数大于得分范围的结果，返回对应的结果
        ScoringResult maxScoringResult = scoringResultList.get(0);
        for (ScoringResult scoringResult : scoringResultList) {
            int resultScoreRange = scoringResult.getResultScoreRange();
            if (totalScore >= resultScoreRange) {
                maxScoringResult = scoringResult;
                //因为是降序排列，所以找到第一个满足条件的结果就退出循环
                break;
            }
        }
        //4. 构造返回值，填充答案对象的属性
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(appId);
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setResultId(maxScoringResult.getId());
        userAnswer.setResultScore(maxScoringResult.getResultScoreRange());
        userAnswer.setResultName(maxScoringResult.getResultName());
        userAnswer.setResultDesc(maxScoringResult.getResultDesc());
        userAnswer.setResultPicture(maxScoringResult.getResultPicture());
        return userAnswer;
    }
}
